package org.lq.er.view.login;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import org.lq.er.entity.Admin;
import org.lq.er.entity.Student;
import org.lq.er.util.CodePicture;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import java.awt.Font;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.FileOutputStream;

import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
/**
 * 重置密码窗口
 * @author 宋铀
 * 
 * @package org.lq.er.view.login
 * @date 2020年9月8日上午8:59:57
 */
public class V_repasswd2 extends JFrame {

	private JPanel contentPane;
	private JTextField text_phone;
	private JTextField text_code;
	private String code;
	private static Admin admin = null;
	private JLabel lbl_icon;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					V_repasswd2 frame = new V_repasswd2(admin);
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public V_repasswd2(Admin admin) {
		setTitle("找回密码(管里员)");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 422, 278);
		setLocationRelativeTo(null);
		contentPane = new JPanel();
		contentPane.setBackground(Color.white);
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		
		JLabel lbl_phone = new JLabel("手机号码:");
		lbl_phone.setBounds(86, 76, 72, 19);
		lbl_phone.setFont(new Font("宋体", Font.PLAIN, 16));
		
		JLabel lblNewLabel_1 = new JLabel("找回密码");
		lblNewLabel_1.setBounds(171, 23, 108, 31);
		lblNewLabel_1.setFont(new Font("幼圆", Font.PLAIN, 27));
		
		text_phone = new JTextField();
		text_phone.setBounds(182, 76, 134, 21);
		text_phone.setColumns(10);
		
		JLabel lbl_code = new JLabel(" 验证码:");
		lbl_code.setBounds(94, 122, 64, 19);
		lbl_code.setFont(new Font("宋体", Font.PLAIN, 16));
		
		text_code = new JTextField();
		text_code.setBounds(182, 122, 80, 21);
		text_code.setColumns(10);
		
		JButton btnNewButton = new JButton("下一步");
		// 判断输入内容是否正确
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String phone = text_phone.getText();
				String txtCode = text_code.getText();
				String phone_no = admin.getPhone_no();
				if (phone.equals(phone_no)&&code.equalsIgnoreCase(txtCode)) {
					System.out.println("1111");
					V_newPwd2 newPwd = new V_newPwd2(admin);
					newPwd.setBounds(V_repasswd2.this.getX()+100, V_repasswd2.this.getY(), newPwd.getWidth(), newPwd.getHeight());
					newPwd.setVisible(true);	
					dispose();
				} else {
					getPicture(lbl_icon, contentPane);
					JOptionPane.showMessageDialog(V_repasswd2.this, "手机号码或验证码错误!","提示",JOptionPane.ERROR_MESSAGE);
				}
			}
		});
		btnNewButton.setBounds(150, 188, 98, 23);
		
		lbl_icon = new JLabel();
		// 验证码图片单击事件
		lbl_icon.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				if (e.getClickCount() == 1) {
					// 重新获取验证码
					getPicture(lbl_icon, contentPane);
				}
			}
		});
		lbl_icon.setBounds(272, 117, 80, 40);
		Object[] obj = CodePicture.createImage();
		ImageIcon img = new ImageIcon((BufferedImage)obj[1]);
		code = obj[0].toString();
		lbl_icon.setIcon(img);
		
		
		
		contentPane.setLayout(null);
		contentPane.add(lblNewLabel_1);
		contentPane.add(lbl_code);
		contentPane.add(lbl_phone);
		contentPane.add(text_phone);
		contentPane.add(text_code);
		contentPane.add(lbl_icon);
		contentPane.add(btnNewButton);
		
		JLabel lblMsg = new JLabel("点击图片刷新");
		lblMsg.setBounds(272, 167, 80, 15);
		contentPane.add(lblMsg);

	}
	
	// 添加图片,获取验证码
	public String getPicture(JLabel label, JPanel panel) {
		Object[] obj = CodePicture.createImage();
		code = obj[0].toString();
		ImageIcon img = new ImageIcon((BufferedImage)obj[1]);//创建图片对象
		label.setIcon(img);
		contentPane.add(label);
		return code;
	}
	
	
}
